/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.google.common.collect.Lists;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.account.soap.SoapProvisioning;
/*     */ import com.zimbra.soap.admin.message.GetServerStatsRequest;
/*     */ import com.zimbra.soap.admin.message.GetServerStatsResponse;
/*     */ import java.io.IOException;
/*     */ import java.net.Socket;
/*     */ import java.util.List;
/*     */ import junit.framework.TestCase;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestServerStats
/*     */   extends TestCase
/*     */ {
/*     */   private SoapProvisioning prov;
/*  40 */   private List<Socket> sockets = Lists.newArrayList();
/*     */   
/*     */   public void setUp() throws Exception
/*     */   {
/*  44 */     this.prov = TestUtil.newSoapProvisioning();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void testThreadsAndConnections()
/*     */     throws Exception
/*     */   {
/*  53 */     Server server = Provisioning.getInstance().getLocalServer();
/*  54 */     verifyThreadsAndConnections("pop_threads", "pop_conn", server.getPop3BindPort());
/*  55 */     verifyThreadsAndConnections("imap_threads", "imap_conn", server.getImapBindPort());
/*  56 */     verifyThreadsAndConnections("lmtp_threads", "lmtp_conn", server.getLmtpBindPort());
/*     */   }
/*     */   
/*     */   private void verifyThreadsAndConnections(String threadStatName, String connStatName, int port) throws Exception
/*     */   {
/*  61 */     int originalConnections = getStatValue(connStatName);
/*     */     
/*  63 */     Socket socket1 = connect(port);
/*  64 */     Socket socket2 = connect(port);
/*  65 */     int numConnections = originalConnections + 2;
/*  66 */     waitForValue(connStatName, numConnections);
/*     */     
/*  68 */     int numThreads = getStatValue(threadStatName);
/*  69 */     assertTrue("Unexpected thread count: " + numThreads, numThreads > 0);
/*     */     
/*  71 */     socket1.close();
/*  72 */     socket2.close();
/*  73 */     waitForValue(connStatName, originalConnections);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   private void waitForValue(String statName, int expected)
/*     */     throws Exception
/*     */   {
/*  81 */     int val = 0;
/*  82 */     for (int i = 1; i <= 20; i++) {
/*  83 */       val = getStatValue(statName);
/*  84 */       if (val == expected) {
/*  85 */         return;
/*     */       }
/*  87 */       Thread.sleep(50L);
/*     */     }
/*     */     
/*  90 */     assertEquals(statName, expected, val);
/*     */   }
/*     */   
/*     */   private int getStatValue(String statName) throws Exception
/*     */   {
/*  95 */     GetServerStatsResponse stats = (GetServerStatsResponse)this.prov.invokeJaxb(new GetServerStatsRequest(new String[] { statName }));
/*  96 */     String s = stats.getValue(statName);
/*  97 */     if (StringUtil.isNullOrEmpty(s)) {
/*  98 */       return 0;
/*     */     }
/* 100 */     return Integer.parseInt(stats.getValue(statName));
/*     */   }
/*     */   
/*     */   private Socket connect(int port) throws ServiceException, IOException
/*     */   {
/* 105 */     Server server = Provisioning.getInstance().getLocalServer();
/* 106 */     Socket socket = new Socket(server.getName(), port);
/* 107 */     this.sockets.add(socket);
/* 108 */     return socket;
/*     */   }
/*     */   
/*     */   public void tearDown() throws IOException
/*     */   {
/* 113 */     for (Socket socket : this.sockets) {
/* 114 */       if (!socket.isClosed()) {
/* 115 */         socket.close();
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args) throws Exception
/*     */   {
/* 122 */     TestUtil.cliSetup();
/* 123 */     TestUtil.runTest(TestServerStats.class);
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestServerStats.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */